//
//  THBaseViewController.m
//  KidReading
//
//  Created by telen on 15/12/1.
//  Copyright © 2015年 刘赞黄Telen. All rights reserved.
//

#import "THBaseViewController.h"
#import "ConstDefine.h"


@interface THBaseViewController ()<EmptyShowDelegate>
{
    UIView* loadingView;
    UIView* contentview;
    //
    UIView* emptyView;
}
@end

@implementation THBaseViewController
@synthesize contentView = contentview;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
   
    
    CGSize actSize = CGSizeMake(160, 42);
    if (![NSLocalizedString(@"language", "") isEqualToString:@"cn"]) {
        actSize = CGSizeMake(180, 42);
    }
    if([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
        actSize = CGSizeMake(130, 30);
        if (![NSLocalizedString(@"language", "") isEqualToString:@"cn"]) {
            actSize = CGSizeMake(150, 30);
        }
    }
    self.countActivity = [[CountingActivity alloc] initWithSize:actSize InView:self.contentView];
    self.wattingShow = [WaitingShow createInstance];
    self.wattingShow.toQueueArr = NO;
    self.wattingShow.notToSaveAttr = NO;
//    if([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
//       self.wattingShow.centerOffset = CGPointMake(0, -30);
//    }
    [self createEmptyShow];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.contentView bringSubviewToFront:self.countActivity];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UIView*)contentView
{
    if (!contentview) {
        contentview = [[UIView alloc] initWithFrame:self.view.bounds];
        contentview.backgroundColor = [UIColor clearColor];
        [self.view addSubview:contentview];
    }
    return contentview;
}

#pragma mark-
#pragma mark- loadingView
- (void)removeLoadingView
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(addLoadingView_) object:nil];
    @synchronized(self){
        if (loadingView) {
            [UIView animateWithDuration:0.2f animations:^{
                loadingView.alpha = 0;
            } completion:^(BOOL finished) {
                [loadingView removeFromSuperview];
                loadingView = nil;
            }];
        }
    }
}

- (void)addLoadingView_EmptyData
{
    if(!_didDisplay_Data)[self addLoadingView_];
}

- (void)addLoadingView_rightNow
{
    [self addLoadingView_];
}

- (void)addLoadingView
{
    [DataProcessing do_Main:^{
        [self addLoadingView_];
    } delay:0.3f];
}
- (void)addLoadingView_
{
    if (!loadingView) {
        loadingView = [[UIView alloc] initWithFrame:self.view.bounds];
        UIImage* image = [UIImage imageNamed:@"bookcase_bg_cell.png"];
        loadingView.backgroundColor = [UIColor colorWithPatternImage:image];
        [self.view addSubview:loadingView];
        loadingView.translatesAutoresizingMaskIntoConstraints = NO;
        [loadingView lyt_alignToParent];
        //
        image = [UIImage imageNamed:@"public_loading01.png"];
        UIImageView* imgvN = [[UIImageView alloc] init];
        imgvN.animationImages = [NSArray arrayWithObjects:image,[UIImage imageNamed:@"public_loading02.png"],[UIImage imageNamed:@"public_loading03.png"],[UIImage imageNamed:@"public_loading04.png"],[UIImage imageNamed:@"public_loading05.png"],[UIImage imageNamed:@"public_loading06.png"],[UIImage imageNamed:@"public_loading07.png"],[UIImage imageNamed:@"public_loading08.png"], nil];
        imgvN.animationDuration = 0.8f;
        imgvN.animationRepeatCount = 0;//forever
        [loadingView addSubview:imgvN];
        [imgvN startAnimating];
        imgvN.translatesAutoresizingMaskIntoConstraints = NO;
        [imgvN lyt_setSize:image.size];
        [imgvN lyt_centerInParent];
    }
    if (loadingView) {
        [self.view bringSubviewToFront:loadingView];
    }
}

- (void)addEmptyView
{
    if (!emptyView) {
        emptyView = [[UIView alloc] init];
        emptyView.backgroundColor = self.view.backgroundColor;
        [self.view addSubview:emptyView];
        emptyView.translatesAutoresizingMaskIntoConstraints = NO;
        [emptyView lyt_alignToParent];
        //
        UIImage* image = [UIImage imageNamed:@"load_failed.png"];
        UIImageView* imgvN = [[UIImageView alloc] init];
        imgvN.image = image;
        [emptyView addSubview:imgvN];
        [imgvN startAnimating];
        imgvN.translatesAutoresizingMaskIntoConstraints = NO;
        [imgvN lyt_setSize:image.size];
        [imgvN lyt_centerInParent];
        //
        UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapEmpty)];
        [emptyView addGestureRecognizer:tap];
    }
}

- (void)tapEmpty {
    
}

- (void)removeEmptyViewView
{
    if (emptyView) {
        @synchronized(self){
            [UIView animateWithDuration:0.2f animations:^{
                emptyView.alpha = 0;
            } completion:^(BOOL finished) {
                [emptyView removeFromSuperview];
                emptyView = nil;
            }];
        }
    }
}

- (void)tapEmptyShow {
    
}

- (void)createEmptyShow {
    self.emptyShow = [[EmptyShow alloc] initWithFrame:self.contentView.bounds];
    [self.contentView addSubview:self.emptyShow];
    //self.emptyShow.translatesAutoresizingMaskIntoConstraints = NO;
    //[self.emptyShow lyt_alignToParent];
    self.emptyShow.emptyDelegate = self;
    //
    CGFloat activeTop = 410.0;
    NSInteger lineWidth = 2.0;
    CGFloat viewW = 30.0;
    if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
        activeTop = 265.0;
        lineWidth = 1.0;
        viewW = 25.0;
    }
    //self.emptyShow.topActivety = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    //self.emptyShow.topActivety.translatesAutoresizingMaskIntoConstraints = NO;
    self.emptyShow.topActivety = [[ZYCircleActivity alloc] initWithFrame:CGRectMake((self.emptyShow.frame.size.width-viewW)/2, activeTop, viewW, viewW)];
    self.emptyShow.topActivety.colors = @[[UIColor grayColor]];
    [self.emptyShow addSubview:self.self.emptyShow.topActivety];
    self.emptyShow.topActivety.alpha = 0.4;
    self.emptyShow.topActivety.progressLineWidth = lineWidth;
    //
    
//    [self.emptyShow.topActivety lyt_alignTopToView:self.emptyShow margin:activeTop];
//    [self.emptyShow.topActivety lyt_centerXInParent];
//    [self.emptyShow.topActivety lyt_setSize:CGSizeMake(30, 30)];
}


- (void)cleanMemory{}

- (BOOL)floatNum:(CGFloat)a Equal:(CGFloat)b
{
    if (fabs(a-b) < 0.001) {
        return YES;
    }
    return NO;
}

#pragma mark-
- (NSInteger)getGridVipExpireDays:(NSInteger)bsid_
{
//    BOOL ret = [[CacheData shareSingle].getUserLoginInfo isPartsVipEnable_bsId:bsid_];
//    if (ret) {
//        return 1;
//    }
    return 0;
}

@end
